<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="button">开关</button></button>
    <script>
        function Light() {
            this.offLightState = new OffLightState(this)
            this.weakLightState = new WeakLightState(this)
            this.strongLightState = new StrongLightState(this)
            this.button = button
            this.currentState = this.offLightState
        }

        Light.prototype.init = function () {
            this.button.onclick = () => {
                this.currentState.buttonWasPressed()
            }
        }

        Light.prototype.setState = function (newState) {
            this.currentState = newState
        }

        class OffLightState {
            constructor(light) {
                this.light = light
            }
            buttonWasPressed() {
                console.log('弱光')
                this.light.setState(this.light.weakLightState)
            }

        }
        class WeakLightState {
            constructor(light) {
                this.light = light
            }
            buttonWasPressed() {
                console.log('强光')
                this.light.setState(this.light.strongLightState)
            }
        }
        class StrongLightState {
            constructor(light) {
                this.light = light
            }
            buttonWasPressed() {
                console.log('关灯')
                this.light.setState(this.light.offLightState)
            }
        }

        var light = new Light()
        light.init()
    </script>
</body>

</html>